package com.beiming.libra.controller;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.UserRelationEnum;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.domain.dto.requestdto.UserProxyAptitudeRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserProxyRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRelationRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRequestDTO;
import com.beiming.libra.domain.dto.responsedto.UserProxyResponseDTO;
import com.beiming.libra.domain.dto.responsedto.UserRelationResponseDTO;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.UserProxyService;
import com.beiming.libra.service.UserRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Rimon Date : 2018/6/5 11:14
 */

@RestController
@Api(value = "关联人接口", tags = "关联人接口")
@RequestMapping("/api/userRelation")
public class UserRelationController {

  @Resource
  private UserRelationService userRelationService;

  @Resource
  private UserProxyService userProxyService;

  /**
   * 分页查询关联人员接口
   *
   * @param userRequestDTO 用户实体
   */
  @PostMapping("/getRelation")
  @ApiOperation(value = "查询关联人员信息", notes = "查询关联人员信息")
  public Result<PageResult<UserRelationResponseDTO>> listRelation(UserRequestDTO userRequestDTO) {
    return Result.success(userRelationService.listRelation(userRequestDTO));
  }

  /**
   * 新增当事人
   */
  @PostMapping("/addParty")
  @ApiOperation(value = "新增当事人", notes = "新增当事人")
  public Result addParty(UserRelationRequestDTO userRelationRequestDTO) {
    if (StringUtils.isEmpty(userRelationRequestDTO)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "userRelationDTO");
    }
    if (StringUtils.isEmpty(userRelationRequestDTO.getId())) {
      userRelationService.insertParty(userRelationRequestDTO, userRelationRequestDTO.getUserId());
    }
    return Result.success("操作成功");
  }

  /**
   * 查询关联人员详情
   *
   * @param id 用户Id
   */
  @GetMapping("/getRelationDetail/{id}/{relationType}")
  @ApiOperation(value = "查询关联人员详情", notes = "查询关联人员详情")
  public Result<UserRelationResponseDTO> relationDetail(
      @ApiParam(value = "关联id", required = true) @PathVariable("id") String id,
      @ApiParam(value = "关联人员类型", required = true) @PathVariable("relationType") String relationType) {
    Assert.isNotNull(id, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Assert.isNotNull(relationType, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    if (UserRelationEnum.PARTY.getCode().equals(relationType)) {
      return Result.success(userRelationService.getUserRelationDetail(id));
    } else {
      return Result.success(userProxyService.getUserProxyDetail(id));
    }
  }

  /**
   * 更新关联人员信息 代理人/当事人
   *
   * @param userRelationRequestDTO 关联人员实体
   */
  @PostMapping("/updateRelation")
  @ApiOperation(value = "更新关联人员信息", notes = "更新关联人员信息")
  public Result updateRelation(UserRelationRequestDTO userRelationRequestDTO) {
    Assert.isNotNull(userRelationRequestDTO, ErrorCode.ILLEGAL_PARAMETER, "参数列表为空");
    Assert.isNotNull(userRelationRequestDTO.getRelationType(), ErrorCode.ILLEGAL_PARAMETER, "关联类型为空");
    if (UserRelationEnum.PARTY.getCode().equals(userRelationRequestDTO.getRelationType())) {
      userRelationService.updateParty(userRelationRequestDTO);
    } else {
      userProxyService.updateProxy(userRelationRequestDTO);
    }
    return Result.success();
  }

  /**
   * 删除关联人员
   *
   * @param id 用户Id
   */
  @PostMapping("/deleteRelation/{id}/{relationType}")
  @ApiOperation(value = "删除关联人员信息", notes = "删除关联人员信息")
  public Result deleteRelation(
      @ApiParam(value = "关联id", required = true) @PathVariable("id") String id,
      @ApiParam(value = "关联用户类型", required = true) @PathVariable("relationType") String relationType) {
    if (StringUtils.isEmpty(id) || StringUtils.isEmpty(relationType)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "id或关联类型不能为空");
    }
    if (UserRelationEnum.PARTY.getCode().equals(relationType)) {
      userRelationService.delete(id);
    } else {
      userProxyService.deleteProxy(id);
    }
    return Result.success("操作成功");
  }

  /**
   * 查询代理资质列表
   *
   * @param userId 用户Id
   */
  @GetMapping("/getProxy/{userId}")
  @ApiOperation(value = "查询代理资质列表", notes = "查询代理资质列表")
  public Result<List<UserProxyResponseDTO>> listProxy(
      @ApiParam(value = "用户id", required = true) @PathVariable("userId") String userId) {
    if (StringUtils.isEmpty(userId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "userId不能为空");
    }
    return Result.success(userRelationService.getUserProxy(userId));
  }

  /**
   * 新增代理人信息
   *
   * @param userProxyRequestDTO 代理人信息实体
   */
  @PostMapping("/addProxy")
  @ApiOperation(value = "新增代理人信息", notes = "新增代理人信息")
  public Result addProxy(UserProxyRequestDTO userProxyRequestDTO) {
    if (StringUtils.isEmpty(userProxyRequestDTO.getUserId())) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id不能为空");
    }
    try {
      userProxyService.addProxy(userProxyRequestDTO);
    } catch (Exception e) {
      return Result.failed(ErrorCode.UNEXCEPTED, e.getMessage());
    }
    return Result.success("操作成功");
  }

  /**
   * 新增代理资质信息
   *
   * @param userProxyRequestDTO 代理人信息实体
   */
  @ApiOperation(value = "新增代理资质信息", notes = "新增代理资质信息")
  @PostMapping("/addProxyAptitude")
  public Result addProxyAptitude(@RequestBody UserProxyAptitudeRequestDTO userProxyRequestDTO) {
    if (StringUtils.isEmpty(userProxyRequestDTO)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "参数为空");
    }
    try {
      userProxyService.addProxyAptitude(userProxyRequestDTO);
    } catch (Exception e) {
      return Result.failed(ErrorCode.UNEXCEPTED, e.getMessage());
    }
    return Result.success("操作成功");
  }

  /**
   * 根据关联人员类型查询当事人/代理人
   */
  @GetMapping("/getRelationByType/{userId}/{relationType}/{pageSize}/{pageNo}")
  @ApiOperation(value = "根据类型查询关联人员", notes = "0：当事人,1：代理人")
  public Result<PageResult<UserRelationResponseDTO>> getRelationByType(@ApiParam(value = "关联人员类型", required = true) @PathVariable("relationType") String relationType,
      @ApiParam(value = "用户id", required = true) @PathVariable("userId") String userId,
      @ApiParam(value = "每页显示数", required = true) @PathVariable("pageSize") int pageSize,
      @ApiParam(value = "页码", required = true) @PathVariable("pageNo") int pageNo) {
    if (StringUtils.isEmpty(relationType) || StringUtils.isEmpty(userId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id或关联人员类型不能为空");
    }
    if (UserRelationEnum.PARTY.getCode().equals(relationType)) {
      return Result.success(userRelationService.listParty(userId, pageSize, pageNo));
    } else {
      return Result.success(userProxyService.listParty(userId, pageSize, pageNo));
    }
  }

  /**
   * 查询当事人列表
   */
  @GetMapping("/getParty/{userId}")
  @ApiOperation(value = "查询当事人列表", notes = "查询当事人列表")
  public Result<List<UserRelationResponseDTO>> getUserRelations(
      @ApiParam(value = "用户id", required = true) @PathVariable("userId") String userId) {
    if (StringUtils.isEmpty(userId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id不能为空");
    }
    return Result.success(userRelationService.getUserRelations(userId));
  }

  /**
   * 查询代理人列表
   */
  @GetMapping("/getProxyUser/{userId}")
  @ApiOperation(value = "查询代理人列表", notes = "查询代理人列表")
  public Result<List<UserRelationResponseDTO>> getProxy(@ApiParam(value = "用户id", required = true) @PathVariable("userId") String userId) {
    if (StringUtils.isEmpty(userId)) {
      return Result.failed(ErrorCode.ILLEGAL_PARAMETER, "用户id不能为空");
    }
    return Result.success(userProxyService.listAllProxy(userId));
  }

}
